package br.ufc.DAO.Implements;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;

import br.ufc.DAO.ICursoDao;
import br.ufc.si.model.Curso;
import br.ufc.util.HibernateUtil;

public class CursoDAO implements ICursoDao {

	public void save(Curso a) {
		Session sessao = HibernateUtil.getSession();

		Transaction txt = sessao.beginTransaction();

		try {
			sessao.save(a);
			txt.commit();
		} catch (Exception e) {
			txt.rollback();
		} finally {
			sessao.close();
		}

	}

	public void update(Curso a) {
		Session sessao = HibernateUtil.getSession();

		Transaction txt = sessao.beginTransaction();

		try {
			sessao.update(a);
			txt.commit();
		} catch (Exception e) {
			txt.rollback();
		} finally {
			sessao.close();
		}

	}

	public void delete(Curso a) {
		Session sessao = HibernateUtil.getSession();

		Transaction txt = sessao.beginTransaction();

		try {
			sessao.update(a);
			txt.commit();
		} catch (Exception e) {
			txt.rollback();
		} finally {
			sessao.close();
		}

	}

	public List<Curso> listALL() {
		Session session = HibernateUtil.getSession();
		try {
			Criteria criteria = session.createCriteria(Curso.class);
			return (List<Curso>) criteria.list();
		} catch (Exception e) {

		}
		return null;
	}

	public Curso listCursoById(int codigo) {
		Session session = HibernateUtil.getSession();
		String query = "from Curso where codigo = :codigo";
		try {
			return (Curso) session.createQuery(query)
					.setInteger("codigo", codigo).uniqueResult();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return null;
	}

}
